﻿using Microsoft.VisualBasic;
using static _01_Tower2._0.MapOld;
using System.Threading;

namespace _01_Tower2._0
{
    class Hero : Character
    {
        Dir dir;
        Dir dir2;
        ConsoleKeyInfo key;
        MapOld map;
        static public Pos pos;
        int monNum, tooNum;

        public int atk, def, hp;
        int damage;
        Random rand;

        CombatSystem cs;
        ToolMenu toolMenu;
        Game game;

        public struct Pos()
        {
            public int x;
            public int y;
            public int x0;
            public int y0;
        }

        enum Dir
        {
            None,
            Up,
            Down,
            Left,
            Right,
        };
        public Hero(string name, int hp, int mp, int atk, int def) : base(name, hp, mp, atk, def)
        {
        }

        public void Init(MapOld map, CombatSystem cs,ToolMenu toolMenu, Game game)
        {
            this.map = map;
            this.cs = cs;
            this.toolMenu = toolMenu;
            this.game = game;
            pos.x0 = pos.x;
            pos.y0 = pos.y;
            monNum = tooNum = 1;
            rand = new Random();
            atk = rand.Next(11, 20);
            def = 20;
            hp = 20;
            damage = 0;
        }
        void Move()
        {
            //Utility.MyPrint(" ", pos.x0, pos.y0);
            switch (dir)
            {
                case Dir.Up:
                    pos.y--;
                    break;
                case Dir.Down:
                    pos.y++;
                    break;
                case Dir.Left:
                    pos.x--;
                    break;
                case Dir.Right:
                    pos.x++;
                    break;
            }
        }
        public void Input()
        {
            dir = Dir.None;
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.UpArrow) || InputSystem.Instance.IsKeyPressed(ConsoleKey.W))
            {
                dir = Dir.Up;
                dir2 = Dir.Up;
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.DownArrow) || InputSystem.Instance.IsKeyPressed(ConsoleKey.S))
            {
                dir = Dir.Down;
                dir2 = Dir.Down;
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.LeftArrow) || InputSystem.Instance.IsKeyPressed(ConsoleKey.A))
            {
                dir = Dir.Left;
                dir2 = Dir.Left;
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.RightArrow) || InputSystem.Instance.IsKeyPressed(ConsoleKey.D))
            {
                dir = Dir.Right;
                dir2 = Dir.Right;
            }
        }
        Pos GetNextPos()
        {
            Pos next;
            next.x = next.y = 0;
            next.x0 = next.y0 = 0;
            switch (dir)
            {
                case Dir.Up:
                    next.x = pos.x;
                    next.y = pos.y - 1;
                    break;
                case Dir.Down:
                    next.x = pos.x;
                    next.y = pos.y + 1;
                    break;
                case Dir.Left:
                    next.x = pos.x - 1;
                    next.y = pos.y;
                    break;
                case Dir.Right:
                    next.x = pos.x + 1;
                    next.y = pos.y;
                    break;
            }
            return next;
        }
        int GetMapType(Pos pos)
        {
            return map.map[pos.y, pos.x];
        }
        public void Update()
        {
            if (GetMapType(GetNextPos()) == (int)MapType.None)// 前方空气就移动
            {
                Move();
            }
            else if (GetMapType(GetNextPos()) == (int)MapType.Tool)// 前方道具就移动并拾取
            {
                Move();
                InfoSystem.Instance.AddInfo($"获得道具{tooNum}", ConsoleColor.Yellow);
                toolMenu.AddInfo($"道具{tooNum++}", ConsoleColor.Yellow);
                map.map[pos.y, pos.x] = (int)MapType.None;
            }
            else if (GetMapType(GetNextPos()) == (int)MapType.MonSlime)// 前方史莱姆就进入战斗
            {
                if (Game.slime.Hp <= 0)
                {
                    Move();
                    map.map[pos.y, pos.x] = (int)MapType.None;
                    InfoSystem.Instance.AddInfo($"获得史莱姆凝胶", ConsoleColor.Blue);
                    toolMenu.AddInfo($"史莱姆凝胶", ConsoleColor.Blue);
                }
                else if (Game.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (Game.slime.Hp > 0)
                {
                    cs.StartCombat(Game.hero, Game.slime);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)MapType.MonSlime2)// 前方史莱姆就进入战斗
            {
                if (Game.slime2.Hp <= 0)
                {
                    Move();
                    map.map[pos.y, pos.x] = (int)MapType.None;
                    InfoSystem.Instance.AddInfo($"获得史莱姆凝胶", ConsoleColor.Blue);
                    toolMenu.AddInfo($"史莱姆凝胶", ConsoleColor.Blue);
                }
                else if (Game.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (Game.slime2.Hp > 0)
                {
                    cs.StartCombat(Game.hero, Game.slime2);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)MapType.MonSlime3)// 前方史莱姆就进入战斗
            {
                if (Game.slime3.Hp <= 0)
                {
                    Move();
                    map.map[pos.y, pos.x] = (int)MapType.None;
                    InfoSystem.Instance.AddInfo($"获得史莱姆凝胶", ConsoleColor.Blue);
                    toolMenu.AddInfo($"史莱姆凝胶", ConsoleColor.Blue);
                }
                else if (Game.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (Game.slime3.Hp > 0)
                {;
                    cs.StartCombat(Game.hero, Game.slime3);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)MapType.MonSlime4)// 前方史莱姆就进入战斗
            {
                if (Game.slime4.Hp <= 0)
                {
                    Move();
                    map.map[pos.y, pos.x] = (int)MapType.None;
                    InfoSystem.Instance.AddInfo($"获得史莱姆凝胶", ConsoleColor.Blue);
                    toolMenu.AddInfo($"史莱姆凝胶", ConsoleColor.Blue);
                }
                else if (Game.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (Game.slime4.Hp > 0)
                {
                    cs.StartCombat(Game.hero, Game.slime4);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)MapType.MonSlime5)// 前方史莱姆就进入战斗
            {
                if (Game.slime5.Hp <= 0)
                {
                    Move();
                    map.map[pos.y, pos.x] = (int)MapType.None;
                    InfoSystem.Instance.AddInfo($"获得史莱姆凝胶", ConsoleColor.Blue);
                    toolMenu.AddInfo($"史莱姆凝胶", ConsoleColor.Blue);
                }
                else if (Game.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (Game.slime5.Hp > 0)
                {
                    cs.StartCombat(Game.hero, Game.slime5);
                    dir = Dir.None;
                }
            }
            else if (GetMapType(GetNextPos()) == (int)MapType.MonSlime6)// 前方史莱姆就进入战斗
            {
                if (Game.slime6.Hp <= 0)
                {
                    Move();
                    map.map[pos.y, pos.x] = (int)MapType.None;
                    InfoSystem.Instance.AddInfo($"获得史莱姆凝胶", ConsoleColor.Blue);
                    toolMenu.AddInfo($"史莱姆凝胶", ConsoleColor.Blue);
                }
                else if (Game.hero.Hp <= 0)
                {
                    Environment.Exit(0);
                }
                else if (Game.slime6.Hp > 0)
                {
                    cs.StartCombat(Game.hero, Game.slime6);
                    dir = Dir.None;
                }
            }
        }

        public void Escape()
        {
            pos.x --;
            pos.y --;
        }

        public void Render()
        {
            Console.SetCursorPosition(pos.x0, pos.y0);
            Console.WriteLine(" ");
            pos.x0 = pos.x;
            pos.y0 = pos.y;
            Console.SetCursorPosition(pos.x, pos.y);
            if (dir2 == Dir.Up)
            {
                Console.WriteLine("^");
            }
            else if (dir2 == Dir.Down)
            {
                Console.WriteLine("V");
            }
            else if (dir2 == Dir.Left)
            {
                Console.WriteLine("<");
            }
            else if (dir2 == Dir.Right)
            {
                Console.WriteLine(">");
            }

        }
        public void End()
        {

        }
        //public bool Attack(bool fight = true)
        //{
        //    while (true)
        //    {
        //        if (hp > 0 && monster.hp > 0)
        //        {
        //            InfoSystem.Instance.AddInfo($"你对{monster.name}发起了攻击", ConsoleColor.Blue);

        //            if (atk > monster.def)
        //            {
        //                damage = atk - monster.def;
        //                InfoSystem.Instance.AddInfo($"你对{monster.name}造成了{damage}点伤害", ConsoleColor.Blue);

        //                monster.hp -= damage;
        //                InfoSystem.Instance.AddInfo($"{monster.name}还剩{monster.hp}点血", ConsoleColor.Blue);

        //            }
        //            else
        //            {
        //                InfoSystem.Instance.AddInfo($"你对{monster.name}造成了0点伤害", ConsoleColor.Blue);

        //            }
        //            if (monster.hp > 0 && hp > 0)
        //            {
        //                InfoSystem.Instance.AddInfo($"怪物{monster.name}向你发起了攻击", ConsoleColor.Red);
        //                if (monster.atk > def)
        //                {
        //                    damage = monster.atk - def;
        //                    InfoSystem.Instance.AddInfo($"你受到了{damage}点伤害", ConsoleColor.Red);
        //                    hpbar.SetHp(hp -= damage);
        //                    //hp -= damage;
        //                    InfoSystem.Instance.AddInfo($"你还剩{hp}点血", ConsoleColor.Red);

        //                }
        //                else
        //                {
        //                    InfoSystem.Instance.AddInfo($"你受到了0点伤害", ConsoleColor.Red);

        //                }
        //            }
        //        }

        //        else if (hp <= 0)
        //        {
        //            InfoSystem.Instance.AddInfo($"你被{monster.name}击败了", ConsoleColor.Green);
        //            fight = false;
        //            break;
        //        }
        //        else if (monster.hp <= 0)
        //        {
        //            InfoSystem.Instance.AddInfo($"你击败了怪物{monster.name}", ConsoleColor.Yellow);
        //            fight = true;
        //            break;
        //        }
        //    }
        //    return fight;
        //}
    }
}
